﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace FangChanCrm.Controllers
{
    public class HuiBaoController : Controller
    {

        private readonly It_HuiBaoBiaoService t_HuiBaoBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_BuMenBiaoService t_BuMenBiaoService;
        private readonly It_GangWeiBiaoService t_GangWeiBiaoService;
        private readonly It_ZhanDuiBiaoService t_ZhanDuiBiaoService;
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;
        private readonly It_TuanDuiBiaoService t_TuanDuiBiaoService;
        private readonly It_TuanDuiRenYuanGuanLianBiaoService t_TuanDuiRenYuanGuanLianBiaoService;

        public HuiBaoController(It_HuiBaoBiaoService huiBaoBiaoService, It_RenYuanBiaoService renYuanBiaoService, It_BuMenBiaoService buMenBiaoService, It_GangWeiBiaoService gangWeiBiaoService, It_ZhanDuiBiaoService zhanDuiBiaoService, It_ZhanQuBiaoService zhanQuBiaoService, It_TuanDuiBiaoService tuanDuiBiaoService, It_TuanDuiRenYuanGuanLianBiaoService tuanDuiRenYuanGuanLianBiaoService, IConfiguration configuration)
        {
            t_HuiBaoBiaoService = huiBaoBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            t_BuMenBiaoService = buMenBiaoService;
            t_GangWeiBiaoService = gangWeiBiaoService;
            t_ZhanDuiBiaoService = zhanDuiBiaoService;
            t_ZhanQuBiaoService = zhanQuBiaoService;
            t_TuanDuiBiaoService = tuanDuiBiaoService;
            t_TuanDuiRenYuanGuanLianBiaoService = tuanDuiRenYuanGuanLianBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        

        public IActionResult PCRiBao()
        {
            return View();
        }
        public IActionResult PCBenRenHuiBaoList()
        {
            return View();
        }
        public IActionResult PCZhouBao()
        {
            return View();
        }
        public IActionResult PCYueBao()
        {
            return View();
        }
        public IActionResult PCHuiBaoList()
        {
            return View();
        }


        public string GetHuiBaoLeiXing()
        {
            int id = Convert.ToInt32(Request.Form["huibaoid"]);
            t_HuiBaoBiao t_HuiBaoBiao = t_HuiBaoBiaoService.GetId(id);
            return t_HuiBaoBiao.HuiBaoLeiXing.ToString();
        }

        public string GetHuiBaoRen()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);
            List<t_RenYuanBiao> rylist = new List<t_RenYuanBiao>();
            if (ry.GangWeiId != 1)
            {
                t_GangWeiBiao gw = t_GangWeiBiaoService.GetById(Convert.ToInt32(ry.GangWeiId));
                if (gw.GangWeiMingCheng == "业务员")
                {
                    List<t_RenYuanBiao> rylistzanc = new List<t_RenYuanBiao>();
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                    rylistzanc = t_RenYuanBiaoService.GetXiaoShouShuJuList("30,31", ry.ZhanDuiId.ToString(), ry.ZhanQuId.ToString());
                    for (int i = 0; i < rylistzanc.Count; i++)
                    {
                        rylist.Add(rylistzanc[i]);
                    }
                    rylistzanc = t_RenYuanBiaoService.GetXiaoShouShuJuList("27,28,29", "", ry.ZhanQuId.ToString());
                    for (int i = 0; i < rylistzanc.Count; i++)
                    {
                        rylist.Add(rylistzanc[i]);
                    }
                }
                else if (gw.GangWeiMingCheng == "销售经理" || gw.GangWeiMingCheng == "储备经理")
                {
                    List<t_RenYuanBiao> rylistzanc = new List<t_RenYuanBiao>();
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                    rylistzanc = t_RenYuanBiaoService.GetXiaoShouShuJuList("27,28,29", "", ry.ZhanQuId.ToString());
                    for (int i = 0; i < rylistzanc.Count; i++)
                    {
                        rylist.Add(rylistzanc[i]);
                    }
                }
                else if (gw.GangWeiMingCheng == "储备总监" || gw.GangWeiMingCheng == "部门总监" || gw.GangWeiMingCheng == "战区总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "置业顾问")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                    t_TuanDuiRenYuanGuanLianBiao tdry = t_TuanDuiRenYuanGuanLianBiaoService.GetRenYuan(Convert.ToInt32(ry.Id));
                    t_TuanDuiBiao td = t_TuanDuiBiaoService.Getid(tdry.TuanDuiId);
                    t_RenYuanBiao ryshuju = t_RenYuanBiaoService.GetById(td.JingLiId);
                    rylist.Add(ryshuju);
                }
                else if (gw.GangWeiMingCheng == "案场经理")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "渠道专员")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("36,48,49");
                }
                else if (gw.GangWeiMingCheng == "渠道经理")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("35,48,49");
                }
                else if (gw.GangWeiMingCheng == "渠道总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "人力助理")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("38,48,49");
                }
                else if (gw.GangWeiMingCheng == "人力总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "培训助理")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("40,48,49");
                }
                else if (gw.GangWeiMingCheng == "培训总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "数据助理")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("42,48,49");
                }
                else if (gw.GangWeiMingCheng == "数据总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                else if (gw.GangWeiMingCheng == "财务出纳" || gw.GangWeiMingCheng == "财务会计")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("44,48,49");
                }
                else if (gw.GangWeiMingCheng == "财务总监")
                {
                    rylist = t_RenYuanBiaoService.GetGangWeiList("48,49");
                }
                StringBuilder Json = new StringBuilder();
                Json.Append("[");
                while (rylist.Count > 0)
                {
                    int i = 0;
                    t_RenYuanBiao ryxinxi = rylist[0];
                    t_GangWeiBiao gwshuju = t_GangWeiBiaoService.GetById(Convert.ToInt32(ryxinxi.GangWeiId));
                    Json.Append("{\"id\":\"" + ryxinxi.Id + "\",\"title\":\"" + gwshuju.GangWeiMingCheng + "\",\"subs\":[");
                    for (int j = 0; j < rylist.Count;)
                    {
                        t_RenYuanBiao ryxinxizhi = rylist[j];
                        t_GangWeiBiao gwshujuzhi = t_GangWeiBiaoService.GetById(Convert.ToInt32(ryxinxizhi.GangWeiId));
                        if (gwshujuzhi.GangWeiMingCheng == gwshuju.GangWeiMingCheng)
                        {
                            if (i == 0)
                            {
                                i++;
                                Json.Append("{\"id\":\"" + ryxinxizhi.Id + "\",\"title\":\"" + ryxinxizhi.XingMing + "/" + gwshujuzhi.GangWeiMingCheng + "\"}");
                                rylist.Remove(rylist[j]);
                            }
                            else
                            {
                                Json.Append(",{\"id\":\"" + ryxinxizhi.Id + "\",\"title\":\"" + ryxinxizhi.XingMing + "/" + gwshujuzhi.GangWeiMingCheng + "\"}");
                                rylist.Remove(rylist[j]);
                            }
                        }
                        else
                        {
                            j++;
                        }
                    }
                    Json.Append("]}");
                    if (rylist.Count > 0)
                    {
                        Json.Append(",");
                    }
                    else
                    {

                    }
                }
                Json.Append("]");
                return Json.ToString();
            }
            else
            {
                return "[]";
            }
        }

        public string GetHuiAdd()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            string dangqian = Request.Form["dangqian"];
            string xia = Request.Form["xia"];
            string qita = Request.Form["qita"];
            string zhuangtai = Request.Form["zhuangtai"];
            string huibaorenid = Request.Form["huibaorenid"];
            string[] huibaoshuju = huibaorenid.ToString().Split(",");
            int panduan = 1;
            for (int i = 0; i < huibaoshuju.Length; i++)
            {
                t_HuiBaoBiao huibao = new t_HuiBaoBiao();
                huibao.BenHuiBao = dangqian;
                huibao.XiaoHuiBao = xia;
                huibao.QiTaHuiBao = qita;
                huibao.HuiBaoRen = id;
                huibao.JieShouRen = Convert.ToInt32(huibaoshuju[i]);
                huibao.HuiBaoLeiXing = zhuangtai;
                huibao.HuiBaoShiJian = System.DateTime.Now.ToString();
                huibao.HuiBaoZhuangTai = "未读";
                panduan = t_HuiBaoBiaoService.Add(huibao);
                if (panduan > 0)
                {

                }
                else
                {
                    panduan = 2;
                    break;
                }
            }
            return panduan.ToString();
        }




        public string GetHuiBaoList()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            List<t_HuiBaoBiao> huibaolist = t_HuiBaoBiaoService.GetList(id);
            StringBuilder Json = new StringBuilder();
            string huibaoleixingzhanshi = "";
            for (int i = 0; i < huibaolist.Count; i++)
            {
                if (huibaolist[i].HuiBaoLeiXing == "日报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-success text-white' style='font-size:14px;line-height:50px;float:left'>日报</div> ";
                }
                else if (huibaolist[i].HuiBaoLeiXing == "周报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-warning text-white' style='font-size:14px;line-height:50px;float:left'>周报</div> ";
                }
                else if (huibaolist[i].HuiBaoLeiXing == "月报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-primary text-white' style='font-size:14px;line-height:50px;float:left'>月报</div> ";
                }
                t_RenYuanBiao tijiaoren = t_RenYuanBiaoService.GetById(huibaolist[i].JieShouRen);
                //if (i == 0)
                //{
                Json.Append("<tr style='border: 2px solid #FFFFFF;border-bottom: 2px solid #DCDCDC; border-radius: 5px;'><td hidden='hidden'></td><td style='height: 4rem;padding-top:10px;padding-bottom:5px' onclick='ydufangfa(" + huibaolist[i].Id + ")'><ul style='margin:0px'><li style='width:100%;height:3.5rem;list-style: none;display: flex;justify-content: space-between;'><div>" + huibaoleixingzhanshi + "<div style='padding-left:5px;float:left'><span class='d-inline-block' style='font-size:16px;padding-bottom:5px'>" + tijiaoren.XingMing + "</span><p style='font-size: 13px;color:#A9A9A9;margin:0px'>" + huibaolist[i].HuiBaoShiJian + "</p></div></div><span style='color:#FFA500;line-height:50px;padding-right:5px'>" + huibaolist[i].HuiBaoZhuangTai + "</span></li></ul></td></tr>");
                //}
                //else 
                //{
                //    Json.Append("<tr class='even'><td onclick='ydufangfa(" + huibaolist[i].Id + ")'>" + huibaoleixingzhanshi + "<h5 class='d-inline-block'>" + tijiaoren.XingMing + "</h5></td><td>" + huibaolist[i].HuiBaoShiJian + "</td>");
                //}
            }
            return Json.ToString();
        }
        public string GetBenRenHuiBaoList()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            List<t_HuiBaoBiao> huibaolist = t_HuiBaoBiaoService.BenGetList(id);
            StringBuilder Json = new StringBuilder();
            string huibaoleixingzhanshi = "";
            for (int i = 0; i < huibaolist.Count; i++)
            {
                if (huibaolist[i].HuiBaoLeiXing == "日报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-success text-white' style='font-size:14px;line-height:50px;float:left'>日报</div> ";
                }
                else if (huibaolist[i].HuiBaoLeiXing == "周报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-warning text-white' style='font-size:14px;line-height:50px;float:left'>周报</div> ";
                }
                else if (huibaolist[i].HuiBaoLeiXing == "月报")
                {
                    huibaoleixingzhanshi = "<div class='icon-50 icon-circle align-self-start bg-primary text-white' style='font-size:14px;line-height:50px;float:left'>月报</div> ";
                }
                t_RenYuanBiao tijiaoren = t_RenYuanBiaoService.GetById(huibaolist[i].HuiBaoRen);
                Json.Append("<tr style='border: 2px solid #FFFFFF;border-bottom: 2px solid #DCDCDC; border-radius: 5px;'><td hidden='hidden'></td><td style='height: 4rem;padding-top:10px;padding-bottom:5px' onclick='ydufangfa(" + huibaolist[i].Id + ")'><ul style='margin:0px'><li style='width:100%;height:3.5rem;list-style: none;display: flex;justify-content: space-between;'><div>" + huibaoleixingzhanshi + "<div style='padding-left:5px;float:left'><span class='d-inline-block' style='font-size:16px;padding-bottom:5px'>" + tijiaoren.XingMing + "</span><p style='font-size: 13px;color:#A9A9A9;margin:0px'>" + huibaolist[i].HuiBaoShiJian + "</p></div></div>");/*<span style='color:#FFA500;line-height:50px;padding-right:5px'>" + huibaolist[i].HuiBaoZhuangTai + "</span></li></ul></td></tr>*/
            }
            return Json.ToString();
        }
        public string upHuiBaoList()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var HuiBao = _context.t_HuiBaoBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                HuiBao.HuiBaoZhuangTai = "已读";
                panduan = _context.SaveChanges() > 0;
            }

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string xianshihuibaoxinxi()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            t_HuiBaoBiao t_HuiBaoBiao = t_HuiBaoBiaoService.GetId(id);
            t_RenYuanBiao tijiaoren = t_RenYuanBiaoService.GetById(t_HuiBaoBiao.HuiBaoRen);
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            for (int i = 0; i < 1; i++)
            {
                Json.Append("{\"huiBaoRen\":\"" + tijiaoren.XingMing + "\",\"ben\":\"" + t_HuiBaoBiao.BenHuiBao + "\",\"xia\":\"" + t_HuiBaoBiao.XiaoHuiBao + "\",\"qita\":\"" + t_HuiBaoBiao.QiTaHuiBao + "\",\"shijian\":\"" + t_HuiBaoBiao.HuiBaoShiJian + "\"}");
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string GetHuiBaoListDate() 
        {
            string fasongren = Request.Form["fasongren"].ToString();
            string jieshouren = Request.Form["jieshouren"].ToString();
            string huibaoliexing = Request.Form["huibaoleixing"].ToString();
            return t_HuiBaoBiaoService.GetListWithFenYe(fasongren,jieshouren,huibaoliexing, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

    }
}
